import Vue from "vue";
import VueRouter from "vue-router";

import "vue2-toast/lib/toast.css";
import Toast from "vue2-toast";

import App from "./App.vue";
// leetcode
import LeetCode from "./views/LeetCode";
import LeetCodeHome from "./views/LeetCode/Home.vue";
import LeetCodeRanks from "./views/LeetCode/Ranks";
// tank
import Tank from "./views/Tank";
import TankWheelWar from "./views/Tank/WheelWar";
import TankFinal from "./views/Tank/Final";
import Airank from "./views/Tank/Airank";
import { getSSOTokenInfo } from "@/views/Tank/utils/index.js";
import TankRegister from "./views/Tank/Register";
import TankMap from "./views/Tank/Map";

// view-design
import "view-design/dist/styles/iview.css";

Vue.use(VueRouter);
Vue.use(Toast, {
  type: "center",
  duration: 1000,
});

const routes = [
  {
    path: "/tank",
    component: Tank,
    children: [
      {
        path: "wheel_war",
        name: "WheelWar",
        component: TankWheelWar,
      },
      {
        path: "final",
        name: "Final",
        component: TankFinal,
      },
      {
        path: "ai",
        name: "AI",
        component: Airank,
      },
      {
        path: "register",
        name: "Register",
        component: TankRegister,
      },
      {
        path: "map",
        name: "Map",
        component: TankMap,
      },
    ],
  },
  {
    path: "/",
    component: LeetCode,
    children: [
      {
        path: "",
        name: "Home",
        component: LeetCodeHome,
      },
      {
        path: "/ranks",
        name: "LeetCodeRanks",
        component: LeetCodeRanks,
      },
    ],
  },
];

Vue.config.productionTip = false;

const router = new VueRouter({
  mode: "hash",
  routes,
});

/**
 * 全局路由控制需要sso验证的path
 * pathForSSO：Array 这里填写需要登录SSO才能访问的路由，路由地址存放在数组中
 * 例如：const pathForSSO = ["/tank"]
 */
const pathForSSO = ["/tank/register", "/tank/map"];
router.beforeEach((to, from, next) => {
  const isAuthPath = pathForSSO.some((val) => val === to.path);
  if (isAuthPath) {
    const userInfo = getSSOTokenInfo();
    if (userInfo) {
      next();
    }
  } else {
    next();
  }
});

new Vue({
  router,
  render: (h) => h(App),
}).$mount("#app");
